/*******************************************************************************
 * This file is part of blueprint4j www.blueprint4j.org.
 * 
 *     blueprint4j is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     blueprint4j is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 * 
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with blueprint4j. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package blueprint4j.gui;

import java.io.IOException;

import javax.swing.JComponent;
import javax.swing.JTabbedPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import blueprint4j.utils.BindException;

public class TabbedpaneGroup extends JTabbedPane implements ToolbarGroup.ToolBarInterface,ChangeListener {

	private TabSelectedInterface previous = null;
	private TabSelectedInterface panels[] = null;
	private ToolbarGroup parent = null;

	public TabbedpaneGroup(int tabPlacement,String titles[],TabSelectedInterface p_panels[])
	throws BindException,IOException {
		super(tabPlacement);
		panels = p_panels;
		for (int i = 0;i < p_panels.length;i++) {
			add(titles[i],p_panels[i].getComponent());
			p_panels[i].setTabbedpaneGroup(this);
		}
		previous = panels[0];
		previous.selected();
		addChangeListener(this);
	}

	public boolean save() {
		try {
			for (int i = 0;i < panels.length;i++) {
				if (!panels[i].save()) {
					return false;
				}
			}
			return true;
		} catch (Exception exception) {
			exception.printStackTrace();
			return false;
		}
	}

	public JComponent getComponent() {
		return this;
	}

	public void stateChanged(ChangeEvent e) {
		//TabbedpaneGroup group = (TabbedpaneGroup) e.getSource();
		// save previous
		if (!previous.save()) {
			setSelectedComponent(previous.getComponent());
		} else {
			previous = panels[getSelectedIndex()];
			panels[getSelectedIndex()].selected();
		}
	}

	public void setToolbarGroup(ToolbarGroup group) {
		this.parent = group;
	}

	public void deactivate() {
		panels[getSelectedIndex()].deactivate();
	}

	public void activate() {
		panels[getSelectedIndex()].activate();
	}

	public void setName(String name) {
		if (parent != null) {
			parent.setName(this,name);
		}
	}

	public void activateMe() {
		if (parent != null) {
			parent.setPanel(this);
		}
	}

	public static interface TabSelectedInterface extends SaveableComponent {
		public void selected();
		public void setTabbedpaneGroup(TabbedpaneGroup parent);
		public void activate();
		public void deactivate();
	}
}

